﻿#ifndef SBASLERCAMERACONTROL_H
#define SBASLERCAMERACONTROL_H

#include <QObject>
#include <pylon/PylonIncludes.h>
#include <QImage>
#include <QTimer>
#include <QDebug>
#include <QMutex>
#include <queue>
#include "halconcpp/HalconCpp.h"
#define DOUBLE_MAX 100000
#define DOUBLE_MIN 0

using namespace std;
using namespace Pylon;
using namespace GenApi;

class SBaslerCameraControl : public QObject
{
    Q_OBJECT
public:
    explicit SBaslerCameraControl(QObject *parent = 0);
    ~SBaslerCameraControl();

    enum SBaslerCameraControl_Type{
        Type_Basler_Freerun, //设置相机的内触发
        Type_Basler_Line1, //设置相机的外触发
        Type_Basler_ExposureTime, //设置相机的曝光时间
        Type_Basler_GainRaw, //设置相机的增益
        Type_Basler_AcquisitionFrameRateAbs, //设置相机的频率
        Type_Basler_Width, //图片的宽度
        Type_Basler_Height, //图片的高度
        Type_Basler_LineSource, //灯的触发信号
        Type_Basler_OffsetX,    // offx
        Type_Basler_OffsetY,   // offy
    };
    void initSome();
    void deleteAll();
    QStringList cameras();
    int OpenCamera(QString cameraSN);
    int CloseCamera();

    void setExposureTime(double time); // 设置曝光时间
    int getExposureTime(); // 获取曝光时间
    int getExposureTimeMin(); // 最小曝光时间
    int getExposureTimeMax(); // 最大曝光时间
    // 获取 X  Y 坐标
    int getOffsetX();
    int getOffsetY();

    // 设置 X  Y 坐标
    void setOffsetX(double tmpValue);

    void setOffsetY(double tmpValue);



    // 获取 相机  宽 高
    int getCameraWidth();
    int getCameraHeight();
    // 设置 相机  宽 高
    void setCameraWidth(double tmpValue);
    void setCameraHeight(double tmpValue);


    // 获取 设置相机增益
    int getGain();
    void setCameraGain(double tmpValue);


    void setFeatureTriggerSourceType(QString type); // 设置种类
    QString getFeatureTriggerSourceType(); // 获取种类：软触发、外触发等等

    void setFeatureTriggerModeType(bool on); // 设置模式触发
    bool getFeatureTriggerModeType(); // 获取模式触发
    void SetCamera(SBaslerCameraControl::SBaslerCameraControl_Type index, double tmpValue = 0.0); // 设置各种参数
    double GetCamera(SBaslerCameraControl::SBaslerCameraControl_Type index); // 获取各种参数

    long GrabImage(HalconCpp::HObject& Hobj,int timeout = 2);
    long StartAcquire(); // 开始采集
    long StopAcquire(); // 结束采集

    void UpdateCameraList(); //更新相机列表


    Pylon::CIntegerParameter GetIntegerParameter(GenApi::INode* pNode) const;




	//
	// After the view has been notified about a new frame it can pick it up.
	// It is then removed from the internal queue
	//
	// Returns:
	//  A shared pointer to the latest frame
	//
	HalconCpp::HObject GetFrame();

	//
	// Clears the internal (double buffering) frame queue
	//
	void ClearFrameQueue();
signals:
    void sigCameraUpdate(QStringList list);
    void sigSizeChange(QSize size);
    void sigCameraCount(int count);
    void sigCurrentImage(int count);
private:

    void CopyToImage(CGrabResultPtr pInBuffer, HalconCpp::HObject &OutImage);

    void CopyToImageRGB8(CGrabResultPtr pInBuffer, HalconCpp::HObject &OutImage);
private slots:
    void onTimerGrabImage();
public:
    CInstantCamera m_basler;

    Pylon::CIntegerParameter m_exposureTime;
    Pylon::CIntegerParameter m_gain;

    QStringList m_cameralist;
    QString m_currentMode;
    bool m_isOpenAcquire = false; // 是否开始采集
    bool m_isOpen = false; // 是否打开摄像头
    QSize m_size;
    DeviceInfoList_t devices;
    int  nIndex = 0;
	int  m_nImgeBuff = 0;


    uchar *dataGray;
    uchar *dateR;
    uchar *dateG;
    uchar *dateB;


	QMutex m_FramesMutex;
	std::queue<HalconCpp::HObject> m_Frames;
};


#endif // SBASLERCAMERACONTROL_H
